package com.fz.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fz.entity.GoodsReview;
import com.fz.service.GoodsReviewService;
import com.fz.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 存储商品评论信息 前端控制器
 * </p>
 *
 * @author hyr
 * @since 2025-06-18
 */
@RestController
@RequestMapping("/goodsReview")
public class GoodsReviewController {
    @Autowired
    private GoodsReviewService goodsReviewService;

    // 添加商品评价
    @PostMapping("/add")
    public ResultVo<Boolean> add(GoodsReview review) {
        return goodsReviewService.addReview(review);
    }

    // 分页获取商品评价
    @GetMapping("/page")
    public ResultVo<IPage<GoodsReview>> page(Long productId, Integer page, Integer size) {
        System.out.println("传入的商品id:"+productId+"页数"+size);
        return ResultVo.success(goodsReviewService.getByProductId(productId, page, size));
    }

    // 检查订单是否可以评价
    @GetMapping("/canReview")
    public ResultVo<Boolean> canReview(Long orderId) {
        return goodsReviewService.canReview(orderId);
    }

    // 获取最新评价w
    @GetMapping("/latest")
    public ResultVo<List<GoodsReview>> latest(Long productId, int limit
    ) {
        return ResultVo.success(goodsReviewService.getLatestReviews(productId, limit));
    }

    // 获取平均评分
    @GetMapping("/averageRating")
    public ResultVo<Double> averageRating(Long productId) {
        System.out.println("传入的平均分id:"+productId);
        return ResultVo.success(goodsReviewService.getAverageRating(productId));
    }
    // 新增接口：获取所有商品的评分分布
    @GetMapping("/rating")
    public ResultVo<Map<Long, int[]>> getRatingDistribution() {
        return ResultVo.success(goodsReviewService.getRatingDistribution());
    }
}
